const subscribeFreqSegment = {
  state: {
    usingSubscribeFreqSegment: []
  },
  mutations: {
    setUsingSubscribeFreqSegment (state, data) {
      state.usingSubscribeFreqSegment = data
    },
    pushUsingSubscribeFreqSegment (state, data) {
      let exist = state.usingSubscribeFreqSegment.find(item => item.id === data.id)
      if (exist) return
      state.usingSubscribeFreqSegment.push(data)
    },
    deleteUsingSubscribeFreqSegment (state, data) {
      for (let i in state.usingSubscribeFreqSegment) {
        if (state.usingSubscribeFreqSegment[i].id === data.id) {
          state.usingSubscribeFreqSegment.splice(i, 1)
        }
      }
    }
  },
  actions: {
    async getUsingSubscribeFreqSegment (context) {
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/abs/devices/subscribe_freq_segment/',
        params: {
          enable: true
        }
      })
      context.commit('setUsingSubscribeFreqSegment', response.data)
      // 地图画线
      for (let item of response.data) {
        context.commit('pushStationMapLine', item)
      }
    }
  },
  getters: {
    getSubscribeFreqSegmentChartData: (state) => {
      let res = []
      state.usingSubscribeFreqSegment.forEach(item => {
        res.push({
          type: 'traffic',
          businessCuDevice: [item.caller_cu, item.called_cu],
          workFreq: item.caller_power,
          bandWidth: (item.caller_up_freq_end - item.caller_up_freq_begin) / 1000,
          centerFreq: (item.caller_up_freq_begin + item.caller_up_freq_end) / 2000,
          user: 'com-wave',
          fillStyle: 'rgba(107,175,229,0.4)',
          strokeStyle: 'rgba(107,175,229,1)'
        })
        res.push({
          type: 'traffic',
          businessCuDevice: [item.called_cu, item.called_cu],
          workFreq: item.called_power,
          bandWidth: (item.called_up_freq_end - item.called_up_freq_begin) / 1000,
          centerFreq: (item.called_up_freq_begin + item.called_up_freq_end) / 2000,
          user: 'com-wave',
          fillStyle: 'rgba(107,175,229,0.4)',
          strokeStyle: 'rgba(107,175,229,1)'
        })
      })
      return res
    },
    getMapLineDataSubscribe: (state) => {
      let res = []
      state.usingSubscribeFreqSegment.forEach(item => {
        res.push({
          name: ``,
          coords: [
            [item.caller_station_longitude, item.caller_station_latitude],
            [item.called_station_longitude, item.called_station_latitude]
          ]
        })
      })
      return res
    }
  }
}
export default subscribeFreqSegment
